package com.taru.houseapp.controller;

import com.taru.houseapp.commen.JsonResult;
import com.taru.houseapp.entity.Order;
import com.taru.houseapp.service.IOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    IOrderService orderService;

    /**
     * 提交预约
     * @param order
     * @return
     */
    @RequestMapping(value = "/order",method = RequestMethod.POST)
    public JsonResult insertOrder(@RequestBody Order order){
        JsonResult result=null;
        try{
            int code=orderService.insertOrder(order);
            if(code==1){
                result=new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 租房人查看自己预约
     * @param userId
     * @param code
     * @return
     */
    @RequestMapping(value = "/order",method = RequestMethod.GET)
    public JsonResult queryRentInUserOrder( String userId,
                                 @RequestParam(value = "code",required = false,defaultValue = "") String code){
        JsonResult result=null;
        if(userId ==null || userId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            List<Order> orders=orderService.queryRentInUserOrder(userId,code);
            result=new JsonResult(200,"获取成功",orders);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 预约详情（租房人角度）
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/order/{orderId}",method = RequestMethod.GET)
    public JsonResult queryRentInUserOrderById(@PathVariable("orderId") String orderId){
        JsonResult result=null;
        if(orderId ==null || orderId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            Order order=orderService.queryRentInUserOrderById(orderId);
            result=new JsonResult(200,"获取成功",order);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 取消预约
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/order/noSuccess",method = RequestMethod.PUT)
    public JsonResult updateOrderIsSuccess(String orderId){
        JsonResult result=null;
        if(orderId ==null || orderId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            orderService.updateOrderIsSuccess(orderId);
            result=new JsonResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 租房人确认租房
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/order/success",method = RequestMethod.PUT)
    public JsonResult updateOrderIsRentHouse(String orderId){
        JsonResult result=null;
        if(orderId ==null || orderId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            orderService.updateOrderIsRentHouse(orderId);
            result=new JsonResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 房东查看预约
     * @param userId
     * @param code
     * @return
     */
    @RequestMapping(value = "/order/treated",method = RequestMethod.GET)
    public JsonResult queryOrderByRentOutUser(@Param("userId") String userId,
                                              @RequestParam(value = "code",required = false,defaultValue = "") String code){
        JsonResult result=null;
        if(userId ==null || userId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            List<Order> orders=orderService.queryOrderByRentOutUser(userId,code);
            result=new JsonResult(200,"获取成功",orders);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 房东确认预约
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/order/check",method = RequestMethod.PUT)
    public JsonResult updateRentOutIsSuccess(String orderId){
        JsonResult result=null;
        if(orderId ==null || orderId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            orderService.updateRentOutIsSuccess(orderId);
            result=new JsonResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 房东查看预约详情
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/order/rentout/{orderId}",method = RequestMethod.GET)
    public JsonResult queryRentOutUserOrderById(@PathVariable("orderId") String orderId){
        JsonResult result=null;
        if(orderId ==null || orderId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            Order order=orderService.queryRentOutUserOrderById(orderId);
            result=new JsonResult(200,"获取成功",order);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 判断当前的人能不能预约
     * @param userId
     * @param houseId
     * @return
     */
    @RequestMapping(value = "/order/judge",method = RequestMethod.GET)
    public JsonResult queryOederIsOk(String userId,String houseId){
        JsonResult result=null;
        if(userId ==null || userId.length()<=0 || userId==null || houseId.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            boolean isExist=orderService.queryOederIsOk(userId,houseId);
            result=new JsonResult(200,"获取成功",isExist);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }
}
